package com.yiren.dbaa.module.project_edit.model;

import android.app.Activity;
import android.util.SparseArray;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.LightCombinationEntity;
import com.yiren.backstage.currency.entity.SingleLightEntity;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.backstage.currency.enums.CarStateNameEnum;
import com.yiren.backstage.currency.enums.CarStateValueEnum;
import com.yiren.backstage.db.dao.bean.SynExamBean;
import com.yiren.backstage.db.dao.cache.GradeStandardCache;
import com.yiren.backstage.db.dao.cache.SingleLightCache;
import com.yiren.backstage.db.dao.controller.GradeDbController;
import com.yiren.backstage.db.dao.controller.UserDbController;
import com.yiren.dbaa.module.project_edit.contract.SingleLightContract;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lwb
 * @date : 2022/8/24 10:34
 * @description:
 */
public class SingleLightModel implements SingleLightContract.Model {

    private Activity activity;
    private Gson gson;

    public SingleLightModel(Activity activity) {
        this.activity = activity;
        gson = new Gson();
    }

    @Override
    public List<SingleLightEntity> getAllSingleLight() {
        Map<String, SingleLightEntity> singleLightEntityMap = SingleLightCache.getAllSingleLight();
        List<SingleLightEntity> singleLightEntityList = new ArrayList<>();
        HashMap<String, String> lightstate = new LinkedHashMap<>();
        Iterator<Map.Entry<String, SingleLightEntity>> iterator = singleLightEntityMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, SingleLightEntity> entry = iterator.next();
            SingleLightEntity singleLight = entry.getValue();
            String statevalue = "";
            lightstate = new Gson().fromJson(singleLight.getStateValue(), LinkedHashMap.class);
            for (String key : lightstate.keySet()) {
                if (lightstate.get(key).contains(CarStateValueEnum.OPEN.toString()) || lightstate.get(key).contains(CarStateValueEnum.CLOSE.toString())) {
                    statevalue = statevalue + CarStateNameEnum.valueOf(key).getDescription() + ":" + CarStateValueEnum.valueOf(lightstate.get(key)).getName() + ",";
                } else {
                    statevalue = statevalue + CarStateNameEnum.valueOf(key).getDescription() + ":" + lightstate.get(key) + ",";
                }
            }
            singleLight.setStateValueShow(statevalue);
            singleLightEntityList.add(singleLight);
        }
        return singleLightEntityList;
    }


    @Override
    public void deleteSingleLight(SiteEntity site, SingleLightEntity singleLight) {
        SynExamBean synExamBean;
        UserParamConfigBean userParamConfigBean = LoadConfigApi.loadUserParamConfig(activity);
        if (userParamConfigBean.isPartner()) {
            synExamBean = gson.fromJson(site.getPartneModifyVal(), new TypeToken<SynExamBean>() {
            }.getType());
        } else {
            synExamBean = gson.fromJson(site.getCoachModifyVal(), new TypeToken<SynExamBean>() {
            }.getType());
        }
        if (synExamBean == null) synExamBean = new SynExamBean();
        if (synExamBean.getSingleLightMap() == null) synExamBean.setSingleLightMap(new HashMap<>());
        if (synExamBean.getSingleLightMap().containsKey(singleLight.getId())) {
            if (synExamBean.getSingleLightMap().get(singleLight.getId()).getState() == 1) {
                synExamBean.getSingleLightMap().remove(singleLight.getId());
            } else {
                synExamBean.getSingleLightMap().put(singleLight.getId(), new SynExamBean.ObjectClass(null, 3));
            }
            SingleLightCache.getAllSingleLight().remove(singleLight.getId());
            if (synExamBean.getGradeStandardMap().containsKey(singleLight.getGradeStandardId())) {
                synExamBean.getGradeStandardMap().remove(singleLight.getGradeStandardId());
                GradeStandardCache.getAllGradeStandard().remove(singleLight.getGradeStandardId().hashCode());
            }
        } else {
            synExamBean.getSingleLightMap().put(singleLight.getId(), new SynExamBean.ObjectClass(null, 3));
            SingleLightCache.getAllSingleLight().remove(singleLight.getId());
        }
        if (userParamConfigBean.isPartner()) {
            site.setPartneModifyVal(gson.toJson(synExamBean));
        } else {
            site.setCoachModifyVal(gson.toJson(synExamBean));
        }
        UserDbController.getInstance(false).update(site);
    }

}
